package com.yc.hr.ui;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.CellEditorListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableModel;

import com.yc.hr.biz.BizException;
import com.yc.hr.biz.DeptBiz;
import com.yc.hr.dao.DeptDao;

import scott.util.JdbcTemplate.Page;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.EventObject;
import java.util.List;
import java.util.Map;
import java.util.Vector;

public class DeptPanel extends JPanel {
	
	//private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	private JTable table;

	/**
	 * Create the panel.
	 */
	public DeptPanel() {
		// 设置边界布局
		setLayout(new BorderLayout(0, 0));
		
		JPanel panel = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		flowLayout.setHgap(10);
		
		// frame.getContentPane().add(panel, BorderLayout.NORTH);
		this.add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("部门:");
		panel.add(lblNewLabel);
		
		textField = new JTextField();
		panel.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("位置:");
		panel.add(lblNewLabel_1);
		
		textField_1 = new JTextField();
		panel.add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnNewButton = new JButton("查询");
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("新增");
		
		panel.add(btnNewButton_1);
		
		JScrollPane scrollPane = new JScrollPane();
//		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		this.add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		//////////////////////////////////////////////////
		Vector<String> head = new Vector<>();
		head.add("部门编号");
		head.add("部门名称");
		head.add("部门位置");
		head.add("操作");
		
		data = new Vector<>();
		
		TableModel model = new DefaultTableModel(data, head);
		table.setModel(model);
		
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				query();
			}
		});
		
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
//				Runnable okTask = new Runnable() {
//					@Override
//					public void run() {
//						query();
//						
//					}
//				};
//				DeptEditDlg dialog = new DeptEditDlg(okTask);
				
				//箭头函数简化以上代码, 添加新部门
				DeptEditDlg dialog = new DeptEditDlg(()->query(), null);
				
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		
		// 之前定义的是一个匿名内部类, 因为它只实现了一个接口
		// 现在定义的是一个具名内部类, 因为它集成+实现2个接口
		// 定义一个类, 他继承 AbstractCellEditor(单元格编辑器抽象类), 
		// 同时要实现 TableCellEditor(表格编辑接口) 和 TableCellRenderer(表格渲染接口)
		class MyEeditor extends AbstractCellEditor implements TableCellEditor, TableCellRenderer{
			private static final long serialVersionUID = 1L;
			
			@Override
			public Object getCellEditorValue() {
				// 该方法不需要实现
				return null;
			}

			@Override
			public Component getTableCellRendererComponent(
					JTable table, Object value, boolean isSelected,
					boolean hasFocus, int row, int column) {
				// 返回渲染的对象
				return getPanel(value);
			}

			@Override
			public Component getTableCellEditorComponent(
					JTable table, Object value, boolean isSelected, int row,
					int column) {
				// 返回编辑的对象
				return getPanel(value);
			}
			
			DeptBiz biz = new DeptBiz();
			
			// 定义一个返回 panel 方法, 那么每一行再点击时, 都会创建一个新的panel, 互不影响
			@SuppressWarnings("unchecked")
			private JPanel getPanel(Object value) {
				JPanel panel = new JPanel();
				JButton btn1 = new JButton("修改");
				JButton btn2 = new JButton("删除");
				panel.add(btn1);
				panel.add(btn2);
				// 添加点击事件
				btn1.addActionListener(e->{
					DeptEditDlg dialog = new DeptEditDlg(()->query(), (Map<String, Object>) value);
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
				});
				btn2.addActionListener(e->{
					// alt + / 代码提示
					if(JOptionPane.showConfirmDialog(null, "请确认是否要删除该部门?")
							== JOptionPane.YES_OPTION) {
						try {
							Map<String,Object> dept = (Map<String, Object>) value;
							biz.remove(dept.get("DEPTNO"));
							// 删除成功, 则重新查询, 刷新表格
							query();
						} catch (BizException e1) {
							e1.printStackTrace();
							JOptionPane.showConfirmDialog(null, e1.getMessage());
						}
					}
				});
				return panel;
			}
		}
		// 创建自定义的编辑+渲染对象
		MyEeditor myEditor = new MyEeditor();
		
		// 设置渲染对象 => 不可操作 => 只读
		table.getColumnModel().getColumn(3).setCellRenderer(myEditor);
		// 设置编辑对象 => 响应用户的操作 => 可写
		table.getColumnModel().getColumn(3).setCellEditor(myEditor);
		// 设置行高
		table.setRowHeight(37);
		
		////////////////////////////////////////////////////
		pagePanel = new PagePanel();
		// 将控件放置在南方(下方)
		add(pagePanel, BorderLayout.SOUTH);
		// 假設當前有45行记录
//		pagePanel.setTotal(45);
		pagePanel.setRows(5);
		// 添加事件监听器
		pagePanel.setPageChangeListener(new PagePanel.PageChangeListener() {
			@Override
			public void change(int page) {
				// System.out.println("当前也是" + page);
				// 页面变化, 执行查询
				query();
			}
		});
		
	}
	
	Vector<Vector<Object>> data;
	PagePanel pagePanel;
	public void query() {
		DeptDao dao = new DeptDao();
//		List<Map<String,Object>> list = dao.queryDnameAndLoc(
//				textField.getText(), textField_1.getText());
		Page page = dao.queryDnameAndLoc(textField.getText(), textField_1.getText(),
				pagePanel.getPage(), pagePanel.getRows());
		// 设置这次查询的总行数, 更新分页条
		pagePanel.setTotal((int) page.getTotal());
		// 清空data中原有的数据
		data.clear();
		for(int i=0; i<page.getData().size();i++) {
			Vector<Object> row = new Vector<>();
			row.add(page.getData().get(i).get("DEPTNO"));
			row.add(page.getData().get(i).get("DNAME"));
			row.add(page.getData().get(i).get("LOC"));
			// 设置操作列的值
			row.add(page.getData().get(i));
			data.add(row);
		}
		table.updateUI();
	}
	
	
}
